package cn.edu.ntu.yyl.controller;

import cn.edu.ntu.yyl.vo.ArticleVO;

import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.service.ArticleService;
import cn.edu.ntu.yyl.service.ArticleShowService;
import cn.edu.ntu.yyl.service.CategoryService;
import cn.edu.ntu.yyl.service.OptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexServlet {
	@Autowired
	ArticleService articleService;
	@Autowired
	OptionService optionService ;
	@Autowired
	CategoryService categoryService ;
	@Autowired
	ArticleShowService articleShowService;
	List<ArticleVO> articleVOS;
	String template = "default";
	//首页
	@GetMapping({"/index.html", "/", "/index", "/index/"})
	public String index( Model model, HttpSession session) {
		List<Article> articles = articleService.SelectAllArticle();
		model.addAttribute("articles",articles);
		session.setAttribute("articles",articles);
		List<ArticleCategory> categories = categoryService.SelectAllArticleCategory();
		model.addAttribute("categories",categories);
		session.setAttribute("categories",categories);

		categories = categoryService.SelectTag();
		model.addAttribute("tags",categories);
		session.setAttribute("tags",categories);
		if (optionService.AutoSelect("template")!=null)
		{
			template = optionService.AutoSelect("template") ;
		}
		return template + "/index";
	}
	//用户注册页
	@GetMapping({"/regist", "/regist.html", "/regist/"})
	public String userRegist(){
		return "ucenter/user_register";
	}
	//文章列表页
	@RequestMapping("/articlelist")
	public String articleList(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
		//查询所有文章
		articleVOS = articleShowService.Show(articleService.SelectAllArticle());
		//将所有文章写入model
		model.addAttribute("articleShows", articleVOS);
		//查询所有分类并写入model
		List<ArticleCategory> categories = categoryService.SelectAllArticleCategory();
		model.addAttribute("categories",categories);
		//查询所有标签并写入model
		List<ArticleCategory> tags = categoryService.SelectTag();
		model.addAttribute("tags",tags);
		return template +"/articlelist";
	}
	//文章归档页
	@RequestMapping("/articlefile")
	public String articleFile(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
		return template +"/articlefile";
	}
	//标签下文章
	@RequestMapping("/tagarticle")
	public String tagarticle(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){

		Integer tagid = 0;
		ArticleCategory tag;
		if (request.getParameter("tagid")!=null){
			//查询选中的标签
			tagid = Integer.valueOf(request.getParameter("tagid"));
			tag = categoryService.SelectTagById(tagid);
			if (tag!=null){
				//查询到了有选择的标签,按照标签查询文章

				articleVOS = articleShowService.Show(articleService.SelectArticleByTag(tag));
				model.addAttribute("articleShows", articleVOS);
				model.addAttribute("tag",tag);
			} else return template + "/articlelist";
		}
		return template +"/tagarticle";
	}
	//分类下文章
	@RequestMapping("/categoryarticle")
	public String categoryarticle(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){

		Integer categoryid = 0;
		ArticleCategory category;
		if (request.getParameter("categoryid")!=null){
			//查询选中的分类
			categoryid = Integer.valueOf(request.getParameter("categoryid"));
			category = categoryService.SelectCategoryById(categoryid);
			if (category!=null){
				//查询到了有选择的分类,按照分类查询文章

				articleVOS = articleShowService.Show(articleService.SelectArticleByCategory(category));
				model.addAttribute("articleShows", articleVOS);
				model.addAttribute("category",category);
			} else return template + "/articlelist";
		}
		return template +"/categoryarticle";
	}
	//搜索页面
	@RequestMapping("/search")
	public String search(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){

		Integer categoryid = 0;
		ArticleCategory category;
		if (request.getParameter("categoryid")!=null){
			//查询选中的分类
			categoryid = Integer.valueOf(request.getParameter("categoryid"));
			category = categoryService.SelectCategoryById(categoryid);
			if (category!=null){
				//查询到了有选择的分类,按照分类查询文章

				articleVOS = articleShowService.Show(articleService.SelectArticleByCategory(category));
				model.addAttribute("articleShows", articleVOS);
				model.addAttribute("category",category);
			} else return template + "/articlelist";
		}
		return template +"/search";
	}
	//搜索动作
	@RequestMapping("/search.do")
	public String DoSearch(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){

		Integer categoryid = 0;
		ArticleCategory category;
		if (request.getParameter("categoryid")!=null){
			//查询选中的分类
			categoryid = Integer.valueOf(request.getParameter("categoryid"));
			category = categoryService.SelectCategoryById(categoryid);
			if (category!=null){
				//查询到了有选择的分类,按照分类查询文章

				articleVOS = articleShowService.Show(articleService.SelectArticleByCategory(category));
				model.addAttribute("articleShows", articleVOS);
				model.addAttribute("category",category);
			} else return template + "/articlelist";
		}
		return template +"/search";
	}

}
